#!/bin/sh

################################################################ 
# Expect the following to be stored in myvars.sh
################################################################ 
# WORKDIR=
# JAVA_HOME=
# JAVA_14_HOME=
# JAVA_15_HOME=
# CATALINA_PID=
# CATALINA_BASE=
# CATALINA_HOME=
# ANT_HOME=
# J2EE_HOME=
# MYSQL_HOME=
# WL_HOME=
# BACKUP_LOCATION=
# TO_REMOTE_DIR=
# O2SL_HOME=
# PSEP=
################################################################ 

ppath() {
case "`uname`" in
  CYGWIN*) echo `cygpath -p $1` ;;
  *) echo $i ;;
esac
}

. ./myvars.sh

mycwd=`pwd`

classpath0=""
for i in \
  jython.jar \
  oreilly-servlet-utils.jar \
  javacc.jar \
  commons-logging.jar \
  commons-collections.jar \
  commons-fileupload.jar \
  commons-io.jar \
  java2html.jar \
  lucene-core.jar \
  fixed-p4.jar \
  radeox_base.jar \
  freemarker.jar \
  commons-collections.jar \
  ehcache.jar \
  bsf.jar \
  mysql-driver.jar \
  servlet-api.jar \
  bogus.jar 
do
  classpath0="${O2SL_HOME}/3rdparty/$i${PSEP}$classpath0"
done

# cannot do this, since WLS bundles ant 1.5.3 optional jars
# CLASSPATH="${ANT_HOME}/lib/ant.jar${PSEP}$CLASSPATH"

# CLASSPATH="${ANT_HOME}/lib/ant-launcher.jar${PSEP}$CLASSPATH"
# CLASSPATH="$JAVA_HOME/lib/tools.jar${PSEP}$CLASSPATH"
# CLASSPATH="$J2EE_HOME/lib/j2ee.jar${PSEP}$CLASSPATH"
# CLASSPATH=".${PSEP}$CLASSPATH"

TO_REMOTE_DIR="$WORKDIR/project/to_remote"

CLASSPATH="."

mypath="${ANT_HOME}/bin${PSEP}$JAVA_HOME/bin"
mypath=`ppath $mypath`
PATH=$mypath$PATHSEP$PATH

export PATH CLASSPATH CATALINA_PID CATALINA_BASE WORKDIR \
       JAVA_HOME JAVA_15_HOME JAVA_14_HOME J2EE_HOME \
       ANT_HOME CATALINA_HOME MYSQL_HOME WL_HOME \
       BACKUP_LOCATION \
       TO_REMOTE_DIR O2SL_HOME

echo "
BUILDDIR=$WORKDIR/project/build
DEPLOYDIR=$WORKDIR/project/deploy
TOMCATBASE=$CATALINA_BASE
WORKDIR=$WORKDIR
TOMCATHOME=$CATALINA_HOME
WL_HOME=$WL_HOME
THIRDPARTY_CLASSPATH=$classpath0
3rdpartydir=../../3rdparty
" > build.properties

reconftomcat() {
  ix="$WORKDIR/project/deploy/"
  cp $ix/jdk_extensions/oxy-jdk-extensions.jar $CATALINA_HOME/common/endorsed
  cp $ix/tomcat_extensions/oxy-tomcat-extensions.jar \
     $ix/common/oxy-common.jar $CATALINA_HOME/server/lib
}

setuptomcat() {
  cd ${O2SL_HOME}/software/misc
  ant setup.tomcat
  cd -
}

starttomcat() {
  cd $CATALINA_BASE
  old_java_home=$JAVA_HOME
  JAVA_HOME=$JAVA_15_HOME
  $CATALINA_HOME/bin/catalina.sh start -security
  JAVA_HOME=$old_java_home
  cd -
}

stoptomcat() {
  cd $CATALINA_BASE
  old_java_home=$JAVA_HOME
  JAVA_HOME=$JAVA_15_HOME
  $CATALINA_HOME/bin/catalina.sh stop -force
  JAVA_HOME=$old_java_home
  cd -
}

jython() {
  java -classpath "$mycp$PSEP$CLASSPATH$PSEP${O2SL_HOME}/3rdparty/jython.jar" org.python.util.jython
}

replacetext() {
  s1="$1"
  s2="$2"
  f="$3"
  echo
  mkdir -p ${TMPDIR}
  for i in `cat $f`
  do
    s="s+${s1}+${s2}+g"
    sed "$s" < $i > ${TMPDIR}/replacetext.txt
    mv ${TMPDIR}/replacetext.txt $i
  done
}

insertcopyright() {
  echo "
/* <<< COPYRIGHT START >>>
 * Copyright 2006-Present OxygenSoftwareLibrary.com
 * Licensed under the GNU Lesser General Public License.
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * @author: Ugorji Dick-Nwoke
 * <<< COPYRIGHT END >>>
 */

" > ${TMPDIR}/insertcopyright.txt
  cd ${O2SL_HOME}/software 
  find wiki forum facade wlfacade classcompatibilityinspector \
       common markup  automationagent  misctools \
       tomcat_extensions logging jdk_extensions jython \
       -name "*.java" -a -type f > ${TMPDIR}/insertcopyrightfiles.txt

  for i in `cat ${TMPDIR}/insertcopyrightfiles.txt`
  do
    cat ${TMPDIR}/insertcopyright.txt > ${TMPDIR}/insertcopyrightreplacement.txt
    cat $i >> ${TMPDIR}/insertcopyrightreplacement.txt
    # return
    mv ${TMPDIR}/insertcopyrightreplacement.txt $i
  done
}


currdrive() {
  pwd | cut -c 1-2
}


j15() {
  export JAVA_HOME=${JAVA_15_HOME}
  export PATH=`ppath $JAVA_HOME/bin`"$PATHSEP$PATH"
}

j14() {
  export JAVA_HOME=${JAVA_14_HOME}
  export PATH=`ppath $JAVA_HOME/bin`"$PATHSEP$PATH"
}

k9() {
  k90 clean all
  k91
}

k90() {
  cd ${O2SL_HOME}
  rmt
  
  j14
  cd ${O2SL_HOME}/3rdparty
  ant clean all
  cd ${O2SL_HOME}/software/jdk_extensions
  ant "$@"
  cd ${O2SL_HOME}/software/common
  ant "$@"
  cd ${O2SL_HOME}/software/markup
  ant "$@"
  cd ${O2SL_HOME}/software/jython
  ant "$@"
  cd ${O2SL_HOME}/software/wiki
  ant "$@"
  j15
  cd ${O2SL_HOME}/software/facade
  ant "$@"  
  cd ${O2SL_HOME}/software/forum
  ant "$@"
  cd ${O2SL_HOME}/software/wlfacade
  ant "$@"
  cd ${O2SL_HOME}/software/tomcat_extensions
  ant "$@"
  j14
  # cd ${O2SL_HOME}/../websites/helpers/home
  # ant "$@"  
}

k91() {
  j14
  # cd ${O2SL_HOME}/../websites
  # ant remote.website.forumwiki.override
  # ant javadocs
  # # ant "$@"

  mkdir -p ${TO_REMOTE_DIR}
  ix="$WORKDIR/project/deploy"
  cp   $ix/tomcat_extensions/oxy-tomcat-extensions.jar \
       $ix/markup/oxy-markup.jar \
       $ix/jdk_extensions/oxy-jdk-extensions.jar \
       $ix/common/oxy-common.jar \
       $ix/jython/oxy-jython.jar \
       $ix/facade/oxy-facade.jar \
       $ix/wlfacade/oxy-wlfacade.jar \
       $ix/forum/oxy*forum* \
       $ix/wiki/oxy*wiki*  \
       $ix/wiki/jspwiki*  \
       $ix/3rdparty/freemarker.jar  \
       $ix/3rdparty/lucene-core.jar  \
       $ix/3rdparty/org.suigeneris.jrcs.diff.jar  \
       $ix/3rdparty/commons-logging.jar  \
       $ix/3rdparty/commons-fileupload.jar  \
       $ix/3rdparty/commons-io.jar \
       $ix/3rdparty/ehcache.jar  \
       $ix/3rdparty/bsf.jar  \
       $ix/3rdparty/bsh.jar  \
       $ix/3rdparty/jhlabs-imaging.jar  \
       $ix/3rdparty/radeox_base.jar  \
       $ix/3rdparty/rome.jar  \
       $ix/3rdparty/jdom.jar  \
       $ix/3rdparty/hibernate.jar \
       $ix/3rdparty/antlr.jar  \
       $ix/3rdparty/asm.jar  \
       $ix/3rdparty/asm-attrs.jar  \
       $ix/3rdparty/cglib.jar  \
       $ix/3rdparty/dom4j.jar  \
       $ix/3rdparty/jta.jar \
     ${TO_REMOTE_DIR}
  
#       $ix/website/remote.website.forumwiki.override.jar \
#       $ix/javadocs/javadocs.jar \
}

tom() {
  mkdir -p ${TMPDIR}/tomcat2
  # rm -rf ${TMPDIR}/tomcat2/*
  cd ${TMPDIR}/tomcat2
  mkdir -p conf/jk shared/lib shared/classes logs temp  webapps
  cp $CATALINA_HOME/conf/web.xml conf
  cp $CATALINA_HOME/conf/context.xml conf
  export JAVA_HOME="$JAVA_15_HOME"
  export CATALINA_HOME="$CATALINA_HOME"
  export CATALINA_BASE="${TMPDIR}/tomcat2"
  export PATH=`ppath $CATALINA_HOME/bin`"$PSEP$PATH"
}


depotbackup() {
  ls -1 $BACKUP_LOCATION
  mkdir -p $BACKUP_LOCATION/3rdparty/
  cd ${O2SL_HOME}
  rmt
  # 05_26_2005
  fname=${TMPDIR}/depot.o2sl.`date '+%m_%d_%Y'`.jar
  jar cfM $fname notes remote_host_machine software websites
  cp ${fname} $BACKUP_LOCATION/
}

depotbackup3p() {
  mkdir -p $BACKUP_LOCATION/3rdparty/
  cd ${O2SL_HOME}/3rdparty
  rmt
  cp -R * $BACKUP_LOCATION/3rdparty/
}

fixjars() {
  echo "
commons-codec-1.3.jar commons-codec.jar
commons-collections-3.1.jar commons-collections.jar
commons-fileupload-1.1.1.jar commons-fileupload.jar
commons-httpclient-3.0.1.jar commons-httpclient.jar
commons-io-1.2.jar commons-io.jar
dom4j-1.6.jar dom4j.jar
ehcache-1.2.jar ehcache.jar
hibernate3.jar hibernate.jar
lucene-core-2.0.0.jar lucene-core.jar
mysql-connector-java-3.1.12-bin.jar mysql-driver.jar
org.suigeneris.jrcs.diff-0.3.0.jar org.suigeneris.jrcs.diff.jar
portlet-api-1.0.jar portlet-api.jar
rome-0.8.jar rome.jar
" > /tmp/ugorji.txt

  while read jarname newjarname
  do
    if [ "x$jarname" == "x" ] || [ "x$newjarname" == "x" ]
    then
      continue
    fi
    echo "|||| $jarname .... $newjarname |||| thank you ||||"
    mytmpfile=/tmp/fix.txt
    find . -type f | grep -v '/setenv.sh' | grep -v '/CVS/' | grep -v '/.svn/' | grep -v '/.build/' | xargs grep -l $jarname > $mytmpfile
    cat $mytmpfile
    replacetext $jarname $newjarname $mytmpfile
    fff $mytmpfile
    em $fff
  done <    /tmp/ugorji.txt
}
 
testfacade() {
  sep=";"
  mycp=""
  mycp="$mycp$sep."
  mycp="$mycp$sep$CLASSPATH"
  mycp="$mycp$sep$WORKDIR/project/deploy/3rdparty/jython.jar"
  mycp="$mycp$sep$WORKDIR/project/deploy/jdk_extensions/oxy-jdk-extensions.jar"
  mycp="$mycp$sep$WORKDIR/project/deploy/common/oxy-common.jar"
  mycp="$mycp$sep$WORKDIR/project/deploy/facade/oxy-facade.jar"
  mycp="$mycp$sep$WORKDIR/project/deploy/wlevs_facade/oxy-wlevs-facade.jar"
  mycp="$mycp$sep$WORKDIR/project/deploy/3rdparty/commons-collections.jar"
  #mycp="$mycp$sep"
  
  mkdir -p $TMPDIR/jythoncachedir
  java -classpath "$mycp" "-Dpython.cachedir=$TMPDIR/jythoncachedir" org.python.util.jython
}

